<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

$email = get('email');
$hash = get('hash');

if($email == '' || $hash == ''){
    // allow them to create a new email confirmation
    
    if(Antz_IntelliForm::submitted('verify')){
        $email = post('email');
        $username = post('username');
        
        $error = false;
        if($email != '' && !JFX_Filter::checkEmail($email)){
            $error = true;
            JFX::addFormError('email', $this->Lang('invalid_format'));
        }else{
            $emailMd5 = md5(strtolower($email));
        };
        $found = false;
        if($DB->countRows($CONFIG->dbprefix.'iusers', "username = '{$username}'")>0){
            $found = true;
            $userid = $DB->oneValue($CONFIG->dbprefix.'iusers', 'id', "username = '{$username}'");
        };
        if($DB->countRows($CONFIG->dbprefix.'iusers', "email_md5 = '{$emailMd5}'")>0){
            $found = true;
            $userid = $DB->oneValue($CONFIG->dbprefix.'iusers', 'id', "email_md5 = '{$emailMd5}'");
        };
    
        if($found){
            $uData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}iusers WHERE id = '{$userid}'");
    
            $emailTemplate = 'admin.register.verify.php';
    
            $emailParams = array(
                'login_url' => JFX::getUrlById($this->GetConfig('login_id')),
                'name' => $fName.' '.$lName,
                'email' => $uData['new_email'],
                'username' => $username,
                'password' => $CRYPT->decrypt($uData['pass']),
                'email_verification_url' => JFX::addParamsToUrl(JFX::getUrlById($this->GetConfig('email_verification_id')), array('email'=>$email, 'hash'=>$hash)),
                'email_verification_url_htmlentities' => JFX::addParamsToUrl(JFX::getUrlById($this->GetConfig('email_verification_id')), array('email'=>$email, 'hash'=>$hash), true),
            );
            $msg = $this->ProcessEmailTemplate($emailTemplate, $emailParams);
    
            $toFrom = array(
                'toName' => $fName.' '.$lName,
                'toEmail' => $email
            );
            JFX_Email::sendRaw($subject, $msg, $toFrom, true);
            
            $domain = explode('@', $uData['new_email']);
            $domain = $domain[1];
            JFX::addSuccess($this->Lang('reminder_sent_to', $domain));
            JFX::goLastPage();
        }
    
    
    }
    
    $formFields = array(
	array(
		'type' => 'text',
		'name' => 'username',
		'label' => $this->Lang('username'),
		'value' => post('username')
	),
	array(
		'type' => 'text',
		'name' => 'email',
		'label' => $this->Lang('email'),
		'value' => post('email')
	)
    );
    
    $form = JFX::makeRapidForm($formFields, $this->Lang('verify_email_legend'), 'verify');
    
    echo $form->render();
    return;
    
};

if(!JFX_Filter::checkEmail($email)){
    JFX::addError($this->Lang('invalid_values'));
    JFX::goLastPage();
}

$uData = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}iusers WHERE new_email = '{$email}'");
if(false===$uData || !is_array($uData) || count($uData)==0){
    JFX::addError($this->Lang('invalid_email'));
    JFX::goLastPage();
}

if($uData['new_email_hash']!=$hash){
    JFX::addError($this->Lang('invalid_hash'));
    JFX::goLastPage();
}

// now we have a valid email and confirmation hash
$newParams = array(
    'email_crypt' => $CRYPT->crypt($uData['new_email']),
    'email_md5' => md5(strtolower($uData['new_email'])),
    'new_email' => '',
    'new_email_hash' => '',
    'status' => '1'
);

$DB->update($CONFIG->dbprefix.'iusers', $newParams, "id = '{$uData['id']}'");
JFX::addSuccess($this->Lang('email_confirmed'));

$uData['email'] = $CRYPT->decrypt($uData['new_email']);
$uData['password'] = $CRYPT->decrypt($uData['pass']);

// log them in for courtesy
$USER->login($uData);

// to other modules
$hookParams = array('user_data' => $uData);
JFX::hook('JFX_Module_Iusers.verifyEmailProcess', $hookParams);


JFX::goLastPage();
